package makai.util;

import java.io.Serializable;

/**
 * Used for the division layout.  A value is basically a 
 * number which can either be absolute OR a percentage.
 * 
 * Absolute values are always returned exactly as they are given.
 * 
 * Percentage values will be multiplied directly by a "Max" value, 
 * so they should be pre-divided to their decimal format.
 * 
 * @author Krechevskoy
 * @version 1.0
 */
public class Value implements Serializable, Cloneable {
    private double value;
    private boolean isPerc;
    
    /**
     * Constructs an absolute zero value
     */
    public Value() {
        this(0, false);
    }
    
    /**
     * Constructs an absolute value of the given amount
     */
    public Value(double value) {
        this(value, false);
    }
    
    /**
     * Constructs a value of the given amount.
     * 
     * See class description for difference between absolute and percentile values
     */
    public Value(double value, boolean isPerc) {
        this.value = value;
        this.isPerc = isPerc;
    }
    
    /**
     * Calculates an absolute form of this value based on the given "Scale" value.
     * 
     * The given "Scale" value is what is to be considered 100%.
     * The returned number may exceed this number, and/or may not have the same sign.
     * 
     * Values which are absolute will always return the same amount,
     * regardless of the given "Scale" value.
     */
    public double getValue(double scale){
        if(isPerc){
            return value*scale;
        } else {
            return value;
        }
    }
    
    /**
     * Sets a new value, either absolute or percentile
     */
    public void setValue(double value, boolean isPerc){
        this.value = value;
        this.isPerc = isPerc;
    }
    
    /**
     * @return The raw value, regardless of being percentile or absolute
     */
    public double getRawValue(){
        return value;
    }
    
    /**
     * @param value The raw value, regardless of being percentile or absolute
     */
    public void setRawValue(double value){
        this.value = value;
    }
    
    /**
     * @return If this value is a percentage
     */
    public boolean isPercentage(){
        return isPerc;
    }
    
    /**
     * @param isPerc If this value is a percentage
     */
    public void isPercentage(boolean isPerc){
        this.isPerc = isPerc;
    }

    @Override
    protected Value clone() throws CloneNotSupportedException {
        return new Value(value, isPerc);
    }
}
